from PyQt6.QtWidgets import QLineEdit,QStackedLayout,QWidget,QHBoxLayout,QCheckBox,QSpacerItem,QLabel,QVBoxLayout
from PyQt6.QtWidgets import QApplication,QSizePolicy,QPushButton
from PyQt6.QtCore import QSize,Qt
from PyQt6.QtGui import QIcon
import sys
from utils.Logger import LogPrint
from views.MultiChoiceListView import MultiChoiceListWidget
from views.PdfPreView import PdfPreView
from views.PdfPreviewList import PdfPreviewList
from utils import ImageUtils

class MiddlePrePdfAreaView(QWidget):


    def __init__(self,parent=None,selectedPdfList=None,selectPageNoList=None,
                 latestSelectPdfPath=None,isCombineLargeImage=False):
        super().__init__(parent=parent)

        self.selectedPdfList=selectedPdfList
        self.selectPageNoList=selectPageNoList
        self.isCombineLargeImage=isCombineLargeImage
        self.latestSelectPdfPath=latestSelectPdfPath

        self.setupUi()


    def setupUi(self):

        #左侧，右边pdf文件预览
        pdfPreLayout=QVBoxLayout()
        pdfPreLayout.setAlignment(Qt.AlignmentFlag.AlignHCenter|Qt.AlignmentFlag.AlignTop)
        label = QLabel('输出预览效果')
        label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        # 添加间隔物
        spacerItem = QSpacerItem(20, 10, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Minimum)
        pdfPreLayout.addItem(spacerItem)
        pdfPreLayout.addWidget(label)

        self.stackedLayout = QStackedLayout()  # 创建堆叠布局  
        self.pdfView = PdfPreView(self)
        self.pdfView.setOnLoadFinishCallback(self.onLoadFinishCallback)
        self.pdfComplexView = PdfPreviewList(self)
        self.pdfComplexView.setScrollCallback(self.updateCurrentPageNo)
        self.pdfComplexView.setOnLoadFinishCallback(self.onLoadFinishCallback)
        self.stackedLayout.addWidget(self.pdfView)
        self.stackedLayout.addWidget(self.pdfComplexView)
        label=QLabel('未选择任何文件')
        #label.setSizePolicy(label.sizePolicy().horizontalPolicy, label.sizePolicy().verticalPolicy)  # 设置标签大小策略
        label.setAlignment(Qt.AlignmentFlag.AlignCenter)  # 设置对齐方式为居中
        self.stackedLayout.addWidget(label)
        self.stackedLayout.setCurrentIndex(0)
        
         # 添加间隔物
        self.pdfSpacerItem = QSpacerItem(20, 1, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Minimum)
        pdfPreLayout.addItem(self.pdfSpacerItem)
        pdfPreLayout.addLayout(self.stackedLayout)

        #下面翻页按钮
        pageTurnLayout=QHBoxLayout()
        self.btnTurnLeft=QPushButton()
        self.btnTurnLeft.setFixedSize(QSize(20,20))
        self.btnTurnLeft.setFlat(True)
        # 设置按钮的样式表
        turnLeftImageUrl=ImageUtils.getImageStyleURI('icon_left_turn.png')
        turnLeftPressImageUrl=ImageUtils.getImageStyleURI('icon_left_turn_select.png')
        self.btnTurnLeft.setStyleSheet(f"""
            QPushButton {{ 
                border-image: url({turnLeftImageUrl}); 
            }}
            QPushButton:pressed {{ 
                border-image: url({turnLeftPressImageUrl}); 
            }}
        """)
        self.btnTurnLeft.clicked.connect(self.onClickedBtnRTurnLeft)
        self.editPageNo=QLineEdit()
        self.editPageNo.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.editPageNo.setFixedSize(QSize(50,20))
        self.editPageNo.setText('1')

        self.labelPageCount=QLabel()
        self.labelPageCount.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.labelPageCount.setFixedSize(QSize(20,20))
        self.labelPageCount.setText('/')

        self.btnTurnRight=QPushButton()
        self.btnTurnRight.setFlat(True)
        self.btnTurnRight.setFixedSize(QSize(20,20))
        # 设置按钮的样式表
        turnRightImageUrl=ImageUtils.getImageStyleURI('icon_right_turn.png')
        turnRightPressImageUrl=ImageUtils.getImageStyleURI('icon_right_turn_select.png')
        self.btnTurnRight.setStyleSheet(f"""
            QPushButton {{ 
                border-image: url({turnRightImageUrl}); 
            }}
            QPushButton:pressed {{ 
                border-image: url({turnRightPressImageUrl}); 
            }}
        """)
        self.btnTurnRight.clicked.connect(self.onClickedBtnRTurnRight)
        pageTurnLayout.addWidget(self.btnTurnLeft)
        pageTurnLayout.addWidget(self.editPageNo)
        pageTurnLayout.addWidget(self.labelPageCount)
        pageTurnLayout.addWidget(self.btnTurnRight)

        # 添加拉伸因子，使得按钮位于布局的底部
        pdfPreLayout.addStretch()
        pdfPreLayout.addLayout(pageTurnLayout)


        #self.setStyleSheet("background-color:yellow")
        self.setLayout(pdfPreLayout)

        #加载pdf预览
        self.setPdfPreLayout()

    #设置中部预览布局
    def setPdfPreLayout(self,selectedPdfList=None,selectPageNoList=None,
                 latestSelectPdfPath=None,isCombineLargeImage=None,
                 selectAllPageNo=None):
        
        LogPrint.info('params',selectedPdfList,selectPageNoList,latestSelectPdfPath,isCombineLargeImage,selectAllPageNo)
        
        if selectedPdfList:
            self.selectedPdfList=selectedPdfList
        if selectPageNoList:
            self.selectPageNoList=selectPageNoList
        if isCombineLargeImage is not None:
            self.isCombineLargeImage=isCombineLargeImage
        if latestSelectPdfPath:
            self.latestSelectPdfPath=latestSelectPdfPath
        if selectAllPageNo is not None:
            if selectAllPageNo==True:
                self.selectPageNoList=None
        #如果当前显示未选择文件状态，则不处理下面逻辑
        if self.stackedLayout.currentIndex() ==2 and (self.selectedPdfList is None or len(self.selectedPdfList)==0):
            return
        
        LogPrint.info('self var',self.selectedPdfList,self.selectPageNoList,self.latestSelectPdfPath,self.isCombineLargeImage)
        
        if self.isCombineLargeImage:
            self.stackedLayout.setCurrentIndex(1)
            pageCount=self.pdfComplexView.loadPdf(self.selectedPdfList,self.selectPageNoList)
            if pageCount > 0:
                    self.editPageNo.setText('1')
                    self.labelPageCount.setText('/'+str(pageCount))
            #self.pdfComplexView.setErrorInfoCallback(self.onShowMessage)
        else:
            self.stackedLayout.setCurrentIndex(0)
            pageCount=self.pdfView.loadPdf(self.latestSelectPdfPath,self.selectPageNoList)
            if pageCount > 0:
                self.labelPageCount.setText('/'+str(pageCount))

    def onLoadFinishCallback(self,pageCount,pdfHeight=420):
        LogPrint.debug('pageCount',pageCount,'pdfHeight',pdfHeight)
        self.labelPageCount.setText('/'+str(pageCount))
        # if pdfHeight < 400:
        #     self.pdfSpacerItem.changeSize(100,int((420-pdfHeight)/2))
        # else :
        #     self.pdfSpacerItem.changeSize(100,1)
        self.pdfSpacerItem.changeSize(100,1)

    #设置中部预览布局
    def cleanPdfPreLayout(self):
        self.stackedLayout.setCurrentIndex(2)
        self.editPageNo.setText('0')
        self.labelPageCount.setText('0')
        self.selectedPdfList=None

    def setPdfFilePath(self,selectedPdfList,latestSelectPdfPath):
        self.selectedPdfList=selectedPdfList
        self.latestSelectPdfPath=latestSelectPdfPath

     #点击左侧翻页按钮
    def onClickedBtnRTurnLeft(self,event):
        if self.isCombineLargeImage:
            if self.pdfComplexView:
                currentPageNo=self.pdfComplexView.turnLeftPage()
                self.editPageNo.setText(str(currentPageNo))
                #判断左右翻页按钮是否可用
                if self.pdfComplexView.isTurnLeft():
                    self.btnTurnLeft.setEnabled(True)
                else :
                    self.btnTurnLeft.setEnabled(False)
                if self.pdfComplexView.isTurnRight():
                    self.btnTurnRight.setEnabled(True)
                else :
                    self.btnTurnRight.setEnabled(False)
        else:
            if self.pdfView:
                currentPageNo=self.pdfView.turnLeftPage()
                self.editPageNo.setText(str(currentPageNo))
                #判断左右翻页按钮是否可用
                if self.pdfView.isTurnLeft():
                    self.btnTurnLeft.setEnabled(True)
                else :
                    self.btnTurnLeft.setEnabled(False)
                if self.pdfView.isTurnRight():
                    self.btnTurnRight.setEnabled(True)
                else :
                    self.btnTurnRight.setEnabled(False)
    #点击右侧翻页按钮
    def onClickedBtnRTurnRight(self,event):
        if self.isCombineLargeImage:
            if self.pdfComplexView:
                currentPageNo=self.pdfComplexView.turnRightPage()
                self.editPageNo.setText(str(currentPageNo))
                 #判断左右翻页按钮是否可用
                if self.pdfComplexView.isTurnLeft():
                    self.btnTurnLeft.setEnabled(True)
                else :
                    self.btnTurnLeft.setEnabled(False)
                if self.pdfComplexView.isTurnRight():
                    self.btnTurnRight.setEnabled(True)
                else :
                    self.btnTurnRight.setEnabled(False)
        else:
            if self.pdfView:
                currentPageNo=self.pdfView.turnRightPage()
                self.editPageNo.setText(str(currentPageNo))
                #判断左右翻页按钮是否可用
                if self.pdfView.isTurnLeft():
                    self.btnTurnLeft.setEnabled(True)
                else :
                    self.btnTurnLeft.setEnabled(False)
                if self.pdfView.isTurnRight():
                    self.btnTurnRight.setEnabled(True)
                else :
                    self.btnTurnRight.setEnabled(False)

    #更新当前页面页码
    def updateCurrentPageNo(self,value):
        self.editPageNo.setText(str(value+1))



    